#import "RootTabBarController.h"
#import "Common.h"
#import "Mediator+My.h"
#import "Mediator+HomePage.h"
#import "Mediator+Loan.h"
#import "Mediator+Consume.h"
#import "RootNavigationController.h"
#define RootTabBarItemBadgeRadius 7.5f
@interface RootTabBarItem : UITabBarItem
@end
@implementation RootTabBarItem {
@private
    NSString *_rootTabBarBadgeValue;
    UILabel *_rootTabBarBadgeLabel;
}
-(NSString *)badgeValue {
    return _rootTabBarBadgeValue;
}
-(void)setBadgeValue:(NSString *)badgeValue {
    _rootTabBarBadgeValue = badgeValue;
    [self updateBadgeViewLayout];
}
-(void)updateBadgeViewLayout {
    NSString *viewKey = @"view";
    if(![Common isVariableWithClass:[UITabBarItem class] varName:viewKey]) {
        return;
    }
    UIView *view = [self valueForKey:viewKey];
    if(!view) {
        return;
    }
    if(!_rootTabBarBadgeLabel) {
        _rootTabBarBadgeLabel = [[UILabel alloc] init];
        _rootTabBarBadgeLabel.backgroundColor = [UIColor redColor];
        _rootTabBarBadgeLabel.textColor = [UIColor whiteColor];
        _rootTabBarBadgeLabel.textAlignment = NSTextAlignmentCenter;
        _rootTabBarBadgeLabel.font = [UIFont systemFontOfSize:9];
        _rootTabBarBadgeLabel.layer.masksToBounds = YES;
        _rootTabBarBadgeLabel.layer.cornerRadius = RootTabBarItemBadgeRadius;
        if(view.superview) {
            [view.superview addSubview:_rootTabBarBadgeLabel];
        }
    }
    BOOL isHiden = _rootTabBarBadgeValue ? NO : YES;
    _rootTabBarBadgeLabel.hidden = isHiden;
    if(!isHiden) {
        _rootTabBarBadgeLabel.text = _rootTabBarBadgeValue;
        CGFloat minX = CGRectGetMinX(view.frame) + CGRectGetWidth(view.frame) / 2 + self.imageInsets.left + RootTabBarItemBadgeRadius;
        CGRect frame = CGRectMake(minX, 4, RootTabBarItemBadgeRadius * 2, RootTabBarItemBadgeRadius * 2);
        _rootTabBarBadgeLabel.frame = frame;
    }
}
@end
@interface RootTabBarController ()<UITabBarControllerDelegate>
@end
@implementation RootTabBarController
-(void)viewDidLoad {
    [super viewDidLoad];
    [self _initializeComponent];
}
-(void)_initializeComponent {
    
    self.tabBar.tintColor =ThemeMaoBackgroundColor ;
    self.tabBar.translucent = NO;
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = HexColor(0x413F3F);
    self.delegate = self;
    Mediator *mediator = [Mediator sharedInstance];
    
    UIViewController *homePageVC = [mediator HomePage_mainViewController];
    homePageVC.tabBarItem = [[RootTabBarItem alloc] init];
    homePageVC.tabBarItem.image = [self imageOriginal:@"a_0_1"];
    homePageVC.tabBarItem.selectedImage = [self imageOriginal:@"a_0_2"];
    [homePageVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    homePageVC.tabBarItem.title = @"首页";
    [homePageVC.tabBarItem setImageInsets:UIEdgeInsetsMake(0, -1, 0, 0)];
    RootNavigationController *homePageNavigationController = [[RootNavigationController alloc] initWithRootViewController:homePageVC];
    
    UIViewController *consumeVC = [mediator Consume_mainViewController];
    consumeVC.tabBarItem = [[RootTabBarItem alloc] init];
    consumeVC.tabBarItem.image = [self imageOriginal:@"a_1_1"];
    consumeVC.tabBarItem.selectedImage = [self imageOriginal:@"a_1_2"];
    [consumeVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    consumeVC.tabBarItem.title = @"消费";
    [consumeVC.tabBarItem setImageInsets:UIEdgeInsetsMake(0, -1, 0, 0)];
    RootNavigationController *consumeNavigationController = [[RootNavigationController alloc] initWithRootViewController:consumeVC];
    
    
    UIViewController *myVC = [mediator My_mainViewController];
    myVC.tabBarItem = [[RootTabBarItem alloc] init];
    myVC.tabBarItem.image = [self imageOriginal:@"a_3_1"];
    myVC.tabBarItem.selectedImage = [self imageOriginal:@"a_3_2"];
    [myVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    myVC.tabBarItem.title = @"我的";
    [myVC.tabBarItem setImageInsets:UIEdgeInsetsMake(0, -1, 0, 0)];
    RootNavigationController *myNavigationController = [[RootNavigationController alloc] initWithRootViewController:myVC];
    
    if ([[Mediator sharedInstance] Login_IsShowFourModule]) {
    
        UIViewController *loanVC = [mediator Loan_mainViewController];
        loanVC.tabBarItem = [[RootTabBarItem alloc] init];
        loanVC.tabBarItem.image = [self imageOriginal:@"a_2_1"];
        loanVC.tabBarItem.selectedImage = [self imageOriginal:@"a_2_2"];
        [loanVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
        loanVC.tabBarItem.title = @"借贷";
        [loanVC.tabBarItem setImageInsets:UIEdgeInsetsMake(0, -1, 0, 0)];
        RootNavigationController *loanNavigationController = [[RootNavigationController alloc] initWithRootViewController:loanVC];
        
        [self setViewControllers:@[homePageNavigationController, consumeNavigationController,loanNavigationController,  myNavigationController]];
    }else {
        
        [self setViewControllers:@[homePageNavigationController, consumeNavigationController,  myNavigationController]];
    }
    
    
    
    for (UIViewController *controller in self.viewControllers) {
        [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: self.tabBar.tintColor} forState:UIControlStateNormal];
    }
}
-(UIImage*)imageOriginal:(NSString*)name {
    UIImage *image = [[UIImage imageNamed:name] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return image;
}
#pragma -mark UIApplicationWillEnterForegroundNotification
-(void)applicationWillEnterForegroundNotification:(NSNotification*)notification {
}
#pragma -mark UserDidLoginNotification
-(void)userDidLoginNotification:(NSNotification*)notification {
}
-(void)userDidLogoutNotification:(NSNotification*)notification {
}
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
//    UIViewController *currentController = tabBarController.selectedViewController;
//    UIViewController *secondController = tabBarController.viewControllers[1];
//    if([secondController isEqual:currentController] && [secondController isEqual:viewController]) {
//        [[Mediator sharedInstance] Order_OrderActionrefreshOrderListInfo];
//    }
    return YES;
}
#pragma -mark Autorotate
-(BOOL)shouldAutorotate {
    return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}
#pragma -mark dealloc
-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
